/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Measure.java
 *
 * Created on Oct 30, 2013, 8:37:20 PM
 */
package sims.gui;

import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import sims.dao.MeasureDao;
import sims.entity.TbMeasure;
import sims.library.Validate;

/**
 *
 * @author NINH
 */
public class Measure extends javax.swing.JPanel {

    MeasureDao measureDao;
    Validate valid;

    public Measure() {
        initComponents();
        measureDao = new MeasureDao();
        valid = new Validate();
        loadData();
    }

    public void loadData() {
        tblMeasure.setModel(DisplayAll());
    }

    public DefaultTableModel DisplayAll() {
        Vector vtable = new Vector();
        Vector vrow = new Vector();
        Vector vheader = new Vector();
        List<TbMeasure> measures = measureDao.getAll(TbMeasure.class);

        vheader.add("Id");
        vheader.add("Name");
        vheader.add("Code");

        for (TbMeasure measure : measures) {
            vrow.add(measure.getMeasureId());
            vrow.add(measure.getMeasureName());
            vrow.add(measure.getMeasureCode());
            vtable.add(vrow);
            vrow = new Vector();
        }
        return new DefaultTableModel(vtable, vheader);
    }

    public void Reset() {
        txtId.setText("");
        txtName.setText("");
        txtCode.setText("");
    }
    public void disableTextbox() {
        txtName.setEnabled(false);
        txtCode.setEnabled(false);
    }

    public void enableTextbox() {
        txtName.setEnabled(true);
        txtCode.setEnabled(true);
    }

    public void binding() {
        int row = tblMeasure.getSelectedRow();
        String id = tblMeasure.getValueAt(row, 0).toString();
        String name = tblMeasure.getValueAt(row, 1).toString();
        String code = tblMeasure.getValueAt(row, 2).toString();

        txtId.setText(id);
        txtName.setText(code);
        txtCode.setText(name);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblMeasure = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        btnDelete = new javax.swing.JButton();
        lblCode = new javax.swing.JLabel();
        lblName = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        txtCode = new javax.swing.JTextField();
        btnUpdate = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtId = new javax.swing.JTextField();
        lblId = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createTitledBorder("Measure manager"));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        tblMeasure.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblMeasure);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 1030, 250));

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sims/images/delete.png"))); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        lblCode.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblCode.setForeground(new java.awt.Color(255, 0, 0));

        lblName.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblName.setForeground(new java.awt.Color(255, 0, 0));

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sims/images/edit.png"))); // NOI18N
        btnUpdate.setText("Edit");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sims/images/add.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Measure code: ");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Measure name: ");

        txtId.setEnabled(false);

        lblId.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblId.setForeground(new java.awt.Color(255, 0, 0));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Measure Id: ");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 312, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnDelete, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE))
                            .addComponent(txtCode, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtId, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblId, javax.swing.GroupLayout.PREFERRED_SIZE, 482, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(lblName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblCode, javax.swing.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)))
                .addGap(54, 54, 54))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblId, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblName, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblCode, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(136, Short.MAX_VALUE))
        );

        add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 280, 1030, 260));
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        if (btnAdd.getText().equalsIgnoreCase("add")) {
            Reset();
            enableTextbox();
            btnAdd.setText("Save");
            tblMeasure.setEnabled(false);
            btnUpdate.setEnabled(false);
            btnDelete.setText("Cancel");
        } else {
            try {
                String code = txtCode.getText();
                String name = txtName.getText();

                TbMeasure measure = new TbMeasure();
                measure.setMeasureCode(code);
                measure.setMeasureName(name);

                measureDao.insert(measure);
                JOptionPane.showMessageDialog(null, "Insert success.");
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Insert error.");
            }
            loadData();
            Reset();
            disableTextbox();
            btnAdd.setText("Add");
            tblMeasure.setEnabled(true);
            btnUpdate.setEnabled(true);
            btnDelete.setText("Delete");
        }
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        if (btnDelete.getText().equalsIgnoreCase("delete")) {
            if (tblMeasure.getSelectedRow() == -1) {
                JOptionPane.showMessageDialog(null, "Select measure to delete.");
                return;
            } else {
                int check = JOptionPane.showConfirmDialog(null, "Are you sure");
                if (check == JOptionPane.OK_OPTION) {
                    int rowIndex = tblMeasure.getSelectedRow();
                    int measureId = Integer.parseInt(tblMeasure.getValueAt(rowIndex, 0).toString());
                    TbMeasure measure = (TbMeasure) measureDao.getById(TbMeasure.class, measureId);
                    measureDao.delete(measure);
                    JOptionPane.showMessageDialog(null, "Delete success.");
                    loadData();
                }
            }
        } else {
            tblMeasure.setEnabled(true);
            btnAdd.setText("Add");
            btnAdd.setEnabled(true);
            btnUpdate.setText("Edit");
            btnUpdate.setEnabled(true);
            btnDelete.setText("Delete");
            Reset();
            disableTextbox();
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        // TODO add your handling code here:
        if (btnUpdate.getText().equalsIgnoreCase("edit")) {
            if (tblMeasure.getSelectedRow() == -1) {
                JOptionPane.showMessageDialog(null, "Select measure to edit.");
                return;
            } else {
                binding();
                enableTextbox();
                btnUpdate.setText("Save");
                tblMeasure.setEnabled(false);
                btnAdd.setEnabled(false);
                btnDelete.setText("Cancel");
            }
        } else {
            int id = Integer.parseInt(txtId.getText());
            String name = txtName.getText();
            String code = txtCode.getText();

            TbMeasure measure = new TbMeasure();
            measure.setMeasureId(id);
            measure.setMeasureName(name);
            measure.setMeasureCode(code);

            measureDao.update(measure);
            JOptionPane.showMessageDialog(null, "Update success.");
            loadData();
            Reset();
            disableTextbox();

            btnUpdate.setText("Edit");
            tblMeasure.setEnabled(true);
            btnAdd.setEnabled(true);
            btnDelete.setText("Delete");
        }
    }//GEN-LAST:event_btnUpdateActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCode;
    private javax.swing.JLabel lblId;
    private javax.swing.JLabel lblName;
    private javax.swing.JTable tblMeasure;
    private javax.swing.JTextField txtCode;
    private javax.swing.JTextField txtId;
    private javax.swing.JTextField txtName;
    // End of variables declaration//GEN-END:variables
}
